import 'package:flutter/material.dart';
import 'package:ducafe_ui_core/ducafe_ui_core.dart';
import 'package:get/get.dart';
import '../utils/my_common.dart';
import '../state/user_state.dart';

/// 登录导航弹窗组件
/// 使用非常规命名避免标准模式
class LoginDialogWidget extends StatefulWidget {
  const LoginDialogWidget({super.key});

  @override
  State<LoginDialogWidget> createState() => _LoginDialogWidgetState();
}

class _LoginDialogWidgetState extends State<LoginDialogWidget>
    with TickerProviderStateMixin {
  // 动画控制器
  late AnimationController _animationController;
  late Animation<double> _fadeAnimation;

  @override
  void initState() {
    super.initState();
    _setupAnimations();
  }

  @override
  void dispose() {
    _animationController.dispose();
    super.dispose();
  }

  /// 设置动画
  void _setupAnimations() {
    _animationController = AnimationController(
      duration: const Duration(milliseconds: 300),
      vsync: this,
    );

    _fadeAnimation = Tween<double>(begin: 0.0, end: 1.0).animate(
      CurvedAnimation(parent: _animationController, curve: Curves.easeInOut),
    );

    _animationController.forward();
  }

  /// 关闭弹窗
  void _closeDialog() {
    _animationController.reverse().then((_) {
      Get.back();
    });
  }

  /// 跳转到登录页面
  void _navigateToLoginPage() {
    // 确保用户状态管理已初始化
    if (!Get.isRegistered<UserState>()) {
      Get.put(UserState(), permanent: true);
    }
    // 直接关闭弹窗并导航
    Get.back();
    Get.toNamed('/user-access');
  }

  @override
  Widget build(BuildContext context) {
    return AnimatedBuilder(
      animation: _animationController,
      builder: (context, child) {
        return FadeTransition(
          opacity: _fadeAnimation,
          child: Dialog(
            backgroundColor: Colors.transparent,
            elevation: 0,
            child: _buildDialogContent(),
          ),
        );
      },
    );
  }

  /// 构建弹窗内容
  Widget _buildDialogContent() {
    return Container(
      width: 320.w,
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(20.r),
        border: Border.all(color: MyCommonUtils.green, width: 2.w),
      ),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          // Logo区域
          _buildLogoSection(),
          SizedBox(height: 20.h),
          // 标题和描述
          _buildTitleSection(),
          SizedBox(height: 30.h),
          // 按钮区域
          _buildButtonSection(),
          SizedBox(height: 20.h),
        ],
      ),
    );
  }

  /// 构建Logo区域
  Widget _buildLogoSection() {
    return Column(
      children: [
        // Logo容器
        Container(
          width: 80.w,
          height: 80.w,
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(20.r),
            image: const DecorationImage(
              image: AssetImage('assets/images/logo.png'),
              fit: BoxFit.contain,
            ),
            border: Border.all(color: MyCommonUtils.green, width: 2),
          ),
        ),
        SizedBox(height: 12.h),
        // 应用名称
        Text(
          'Cult',
          style: TextStyle(
            fontSize: 24.sp,
            fontWeight: FontWeight.bold,
            color: MyCommonUtils.green,
          ),
        ),
      ],
    ).paddingTop(20.h);
  }

  /// 构建标题区域
  Widget _buildTitleSection() {
    return Column(
      children: [
        Text(
          'Login Required',
          style: TextStyle(
            fontSize: 20.sp,
            fontWeight: FontWeight.w600,
            color: MyCommonUtils.black,
          ),
        ),
        SizedBox(height: 8.h),
        Text(
          'Please login to access all features and enjoy the full experience of our community.',
          textAlign: TextAlign.center,
          style: TextStyle(
            fontSize: 14.sp,
            color: MyCommonUtils.grey,
            height: 1.4,
          ),
        ),
      ],
    ).paddingHorizontal(24.w);
  }

  /// 构建按钮区域
  Widget _buildButtonSection() {
    return Column(
      children: [
        // 前往登录按钮
        _buildGoToLoginButton(),
        SizedBox(height: 12.h),
        // 取消按钮
        _buildCancelButton(),
      ],
    ).paddingHorizontal(24.w);
  }

  /// 构建前往登录按钮
  Widget _buildGoToLoginButton() {
    return Container(
          width: double.infinity,
          height: 50.h,
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text(
                'Go to Login',
                style: TextStyle(fontSize: 16.sp, fontWeight: FontWeight.w600),
              ),
            ],
          ),
        )
        .card(
          elevation: 0,
          color: MyCommonUtils.green,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(12.r),
          ),
        )
        .inkWell(onTap: _navigateToLoginPage)
        .animate(const Duration(milliseconds: 300), Curves.easeInOut);
  }

  /// 构建取消按钮
  Widget _buildCancelButton() {
    return Container(
          width: double.infinity,
          height: 50.h,
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text(
                'Maybe Later',
                style: TextStyle(
                  color: MyCommonUtils.grey,
                  fontSize: 16.sp,
                  fontWeight: FontWeight.w500,
                ),
              ),
            ],
          ),
        )
        .card(
          elevation: 0,
          color: Colors.transparent,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(12.r),
            side: BorderSide(color: MyCommonUtils.grey, width: 1),
          ),
        )
        .inkWell(onTap: _closeDialog)
        .animate(const Duration(milliseconds: 300), Curves.easeInOut);
  }
}

/// 显示登录弹窗的静态方法
class LoginDialogUtils {
  /// 显示登录弹窗
  static void showLoginDialog() {
    Get.dialog(
      const LoginDialogWidget(),
      barrierDismissible: true,
      barrierColor: Colors.black.withValues(alpha: 0.5),
    );
  }
}
